<?php
session_start();
require_once('../includes/config.php');
require_once('includes/dbfunctions.php');

if (!isset($_SESSION['User'])) {
    require ("login.php");
    exit;
}

if ($_POST)
{
    $pageid = $_POST['pageid'];
    if (($_POST['action']) == "Delete")
    {
        //delete the page
        deleteContent($pageid, 0);
    }

    if (($_POST['action']) == "Unpublish")
    {
        //unpublish the page
        unpublishPage($pageid);
    }
}

$pages = getAllPublishedPages();

$title = "View All Pages";
require('includes/header.php');
?>
<script type="text/javascript">
    $(document).ready(function()
    {
            $(".action").click(function() {
                //initialize datastring
                var dataString="";

                //delete or unpublish?
                var action = $(this).val();

                //for which page?
                var id = $('#pageid').val();

                //set dataString
                dataString += "pageid=" + id + "&action=" + action;
                
                //submit the form with the XMLHTTPRequest
                $.ajax({
                    type: "POST",
                    url: "view.php",
                    data: dataString,
                    success: function(){
                        $('.page_success').show();
                    }
                });
  

            });

    });

</script>

<?php require('includes/sidebar.php'); ?>
<div class="yui-skin-sam">

    <h2>All Pages</h2>

    <div>
        <form name="editPages" method="" action="">
        <input type="hidden" name="pageid" id="pageid" />
        <table id="pagelist">
        <tr>
                <th scope="col">Title</th>
                <th scope="col">User</th>
                <th scope="col">Published On</th>
                <th scope="col">&nbsp;&nbsp;&nbsp&nbsp;&nbsp;Description</th>
                <th scope="col">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Actions</th>
            </tr>
            <?php
            foreach($pages as $page) {
                $id = $page['ID'];
                $alias = $page['Alias'];
                list($date, $time) = split(" ", $page['AuthDate']);
                list($year, $month, $day) = split("-", $date);
                $prettyDate = $month."/".$day."/".$year;
                echo "<tr>\n";
                echo "    <td><a href=\"../preview.php?page=".$alias."\" target=\"_blank\">".$page['Title']."</td>\n";
                echo "    <td>".$page['Author']."</td>\n";
                echo "    <td>".$prettyDate."</td>\n";
                echo "    <td>".$page['Desc']."</td>\n";
                echo "    <td><input name=\"edit\" class=\"edit\" type=\"button\" value=\"Edit\" onclick=\"parent.location='editpage.php?page=$alias';\" /> \n";
                echo "    <input name=\"unpublish\" class=\"action\" type=\"submit\" value=\"Unpublish\" onclick=\"document.editPages.pageid.value='$id';\" /> \n";
                echo "    <input name=\"delete\" class=\"action\" type=\"submit\" value=\"Delete\" onclick=\"document.editPages.pageid.value='$alias';\" /> \n";
                echo "</tr>\n";
            }
            ?>
        </table>
        </form>


    </div>

</div>
<?php require('includes/footer.php'); ?>
